package cn.ac.jaas.isc.vegetable.pbms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcDevice;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcDeviceService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 装置表
 *
 * @author 张高昌
 * @date 2022-05-22 19:51:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcdevice")
@Api(value = "arcdevice", tags = "装置表管理")
public class ArcDeviceController {

    private final ArcDeviceService arcDeviceService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param arcDevice 装置表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pbms_arcdevice_view')")
    public R getArcDevicePage(Page page, ArcDevice arcDevice) {
        return R.ok(arcDeviceService.page(page, Wrappers.query(arcDevice)));
    }


    /**
     * 通过id查询装置表
     *
     * @param deviceId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{deviceId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcdevice_view')")
    public R getById(@PathVariable("deviceId") Integer deviceId) {
        return R.ok(arcDeviceService.getById(deviceId));
    }

    /**
     * 新增装置表
     *
     * @param arcDevice 装置表
     * @return R
     */
    @ApiOperation(value = "新增装置表", notes = "新增装置表")
    @SysLog("新增装置表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcdevice_add')")
    public R save(@RequestBody @Valid ArcDevice arcDevice, HttpServletRequest request) {
        return arcDeviceService.saveDevice(arcDevice, request);
    }

    /**
     * 修改装置表
     *
     * @param arcDevice 装置表
     * @return R
     */
    @ApiOperation(value = "修改装置表", notes = "修改装置表")
    @SysLog("修改装置表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcdevice_edit')")
    public R updateById(@RequestBody @Valid ArcDevice arcDevice, HttpServletRequest request) {
        return arcDeviceService.updateDevice(arcDevice, request);
    }

    /**
     * 通过id删除装置表
     *
     * @param deviceId id
     * @return R
     */
    @ApiOperation(value = "通过id删除装置表", notes = "通过id删除装置表")
    @SysLog("通过id删除装置表")
    @DeleteMapping("/{deviceId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcdevice_del')")
    public R removeById(@PathVariable Integer deviceId) {
        return R.ok(arcDeviceService.removeById(deviceId));
    }


    /**
     * 导出excel 表格
     *
     * @param arcDevice 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pbms_arcdevice_export')")
    public List<ArcDevice> export(ArcDevice arcDevice) {
        return arcDeviceService.list(Wrappers.query(arcDevice));
    }

}
